import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user:JSON.parse(localStorage.getItem('user')),//当前用户信息
    token:localStorage.getItem('token') //存储token字符串
  },
  getters: {
  },
  mutations: {
    /**
     * 登录成功后将会得到用户信息与token
     * @param {*} state 
     * @param {*} payload 
     */
    saveUserState(state,payload){
      state.user = payload.user
      state.token = payload.token
      //存入locastorage
      localStorage.setItem('token',payload.token)
      localStorage.setItem('user',JSON.stringify(payload.user))
    },
    /**
     * 清空用户状态
     */
    clearUserState(state){
      state.user = undefined
      state.token = undefined
      localStorage.clear()
    }
  },
  actions: {
  },
  modules: {
  }
})
